//全局函数，AES解密
globalThis.AESJieMi = function (data, url) {
	data = data.replace(/^\uFEFF/,'');
	let key = /main/.test(url)? '/main00000000000' : '/'+url.split('/')[3].slice(0,15);
	key = CryptoJS.enc.Utf8.parse(key);
	const decrypted = CryptoJS.AES.decrypt(data, key, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	});
	return decrypted.toString(CryptoJS.enc.Utf8)
};

//hema AES解密
globalThis.decrypt3DESWithCBC = function (cipherText) {
    var encrypted = CryptoJS.enc.Base64.parse(cipherText);
    var keyBytes = CryptoJS.enc.Utf8.parse("ZT8g6QH2kS3Xj7G5wG4JtU1F");
    var ivBytes = CryptoJS.enc.Utf8.parse("51518888");

    var decrypted = CryptoJS.TripleDES.decrypt(
        { ciphertext: encrypted },
        keyBytes,
        { 
            iv: ivBytes,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        }
    );
    return decrypted.toString(CryptoJS.enc.Utf8);
}

globalThis.postPlayUrl = function (url,data) {
	const url1 = 'http://49.233.217.152:5500/api/vod/'+url;
	//const data = 'xz=0&vod_map_id='+jid+'&vod_id='+uid+'&collection=1';
	const response = fetch(url1, {
		method: 'POST',
		headers : {
			'Device-Id': 'c1d0ba45ae7a0058',
			'Channel': 'zhuan',
			'token': 'gAAAAABpBajKx23UKeFntbn4O5PrXDiFl6WiC0cjD0UtxtJcSeLNhjnTjL-23irldhi0ZJF2aeJe5c3tP2DMM-WFuTV3UsfT-liGrxsKF3pSZyl5KjvDPEFjD8CC6EdfhxjYMEzuY3HDtPuWXgrpeT1dI7_VaE-RGCVscAHEr3jXawM7_s9ASXy_CvD_ivYa8FvN6kErhQWN9Z2aaKikH_nsT4wI3AF2Go_HHugqaHDdmfjDLCn8kDJNQUCbPYCTP_HjPPRbNxGKIhgjmakvLus0Lhc4Bxi63whEMrRUhjd-jawk6kpadqQN6QZ5hUWJ3WyyniKHUQbc',
			'appid': 'shiguang',
			'Content-Type': 'application/x-www-form-urlencoded',
			'Content-Length': '50',
		},
		body: data
	});
	return decrypt3DESWithCBC(JSON.parse(response).data.replace(/\n/g, ''))
}

//drpy2主体rule对象
var rule = {
	title: '牛牛app',
	host: 'https://ccc.chaojichaojichanga.com:35620',
	hostJs: '',
	headers: {'User-Agent': MOBILE_UA},
	编码: 'utf-8',
	timeout: 5000,

	homeUrl: '/main',
	url: '/list?fyfilter',
	filter_url: 'wd=&order={{fl.by}}&type_id=fyclass&class={{fl.class}}&area={{fl.area}}&year={{fl.year}}&state={{fl.state}}&page=fypage',
	searchUrl: '/list?order=&wd=**&page=fypage',
	detailUrl: '/detail?vod_id=fyid',

	limit: 9,
	double: false,
	class_name: '短剧&电视剧&电影&动漫&综艺&福利&传媒&吃瓜&午夜&热舞&直播',
	class_url: '5&2&1&4&3&6&7&8&9&10&11',
	filter_def: {},

	推荐: $js.toString(() => {
		let khtml = fetch(input);
		let kjson = JSON.parse(AESJieMi(khtml, input));
		VODS = [];
		kjson.data.forEach((item) => {
			if (Array.isArray(item.list) && item.list.length !== 0) {
				 VODS = VODS.concat(item.list) 
				 }
		})
	}),
	一级: $js.toString(() => {
		let khtml = fetch(input);
		let kjson = JSON.parse(AESJieMi(khtml, input));
		VODS = kjson.data
	}),
	搜索: '*',
	二级: $js.toString(() => {
		let khtml = fetch(input);
		let kjson = JSON.parse(AESJieMi(khtml, input));
		let kvod = kjson.data;
		let ktabs = kvod.sources.map((it) => { return '' + it.player_id });
		let kurls = kvod.sources.map((item) => {
			let kurl = item.episodes.map((it) => { return `${it.name}$${it.url}*${item.player_id}` });
			return kurl.join('#')
		});
		VOD = {
			vod_id: kvod.vod_id,
			vod_name: kvod.vod_name,
			vod_pic: kvod.vod_pic,
			type_name: kvod.vod_class,
			vod_remarks: kvod.vod_remarks,
			vod_year: kvod.vod_year,
			vod_area: kvod.vod_area,
			vod_lang: kvod.vod_lang,
			vod_director: kvod.vod_director,
			vod_actor: kvod.vod_actor,
			vod_content: kvod.vod_content,
			vod_play_from: ktabs.join('$$$'),
			vod_play_url: kurls.join('$$$')
		}
	}),

	tab_remove: ['xm3u8'],
	play_parse: true,
	lazy: $js.toString(() => {
		let kurl = input.split('*')[0];
		let ktab = input.split('*')[1];
		if (/\.m3u8/.test(kurl)) {
			input = { jx: 0, parse: 0, url: kurl, header: rule.headers }
		} else if (/hema/.test(ktab)) {
			let uid=kurl.split('@')[0];
			let js=kurl.split('@')[1];
			let js1=js-1;
			let start = Math.floor(Date.now() / 1000).toString(16);
			let jsid = JSON.parse(postPlayUrl("info","vod_id="+uid)).result.map_list[js1].id;
			let jsony = JSON.parse(postPlayUrl("play_url","xz=0&vod_map_id="+jsid+"&vod_id="+uid+"&collection="+js));//.result.vod_url
			let jsurl = jsony.result.vod_url;
			let jm = base64Decode(jsony.result.ck);
			let jm1 = md5(jsurl.replaceAll("http://xs85.ruxiangsuisu.cn","vT1RQRz8YzlzTgN26pIXNJ7Mi65juwSP")+start).toUpperCase();
			kurl = jsurl+"?"+jm+"&wsSecret="+jm1+"&wsTime="+start;
			input = { jx: 0, parse: 0, url: kurl, header: { 'User-Agent': MOBILE_UA, 'Host': 'xs85.ruxiangsuisu.cn:80' }}
		} else if (/hm3u8/.test(ktab)) {
			kurl = `http://82.156.24.206:12345/jx/jx.php?url=${kurl}`;
			kurl = JSON.parse(fetch(kurl)).url;
			input = { jx: 0, parse: 0, url: kurl }
		} else if (/qyzy/.test(ktab)) {
			kurl = `http://ccs.jshh.gzbaoxian.com/jx/qy.php?url=${kurl}`;
			kurl = JSON.parse(fetch(kurl)).url;
			input = { jx: 0, parse: 0, url: kurl }
		} else if (/hmjc/.test(ktab)) {
			kurl = `http://101.42.92.211:5560/jx/dj.php?url=${kurl}`;
			kurl = JSON.parse(fetch(kurl)).url;
			input = { jx: 0, parse: 0, url: kurl, header: { 'User-Agent': MOBILE_UA, 'Referer': 'https://m3u8.com' } }
		} else if (/djzy/.test(ktab)) {
			kurl = `http://ccs.jshh.gzbaoxian.com/jx/dj.php?url=${kurl}`;
			kurl = JSON.parse(fetch(kurl)).url;
			input = { jx: 0, parse: 0, url: kurl, header: rule.headers }
		} else if (/zbjx/.test(ktab)) {
			kurl = `http://192.144.141.22/jx/migu.php?id=${kurl}`;
			kurl = JSON.parse(fetch(kurl)).url;
			input = { jx: 0, parse: 0, url: kurl, header: { 'User-Agent': MOBILE_UA, 'Referer': 'https://m3u8.com'} }
		} else if (/xj/.test(ktab)) {
			kurl = `http://82.156.24.206:12345/jx/xj22.php?id=${kurl}`;
			kurl = JSON.parse(fetch(kurl)).url;
			input = { jx: 0, parse: 0, url: kurl, header: { 'User-Agent': MOBILE_UA, 'Referer': 'https://m3u8.com'} }
		} else if (ktab=="pp") {
			kurl = `http://116.211.150.40:5600/bb/jx.php?id=${kurl}`;
			kurl = JSON.parse(fetch(kurl)).url;
			input = { jx: 0, parse: 0, url:kurl, header: { 'User-Agent': MOBILE_UA, 'Referer': 'https://m3u8.com'} }
		} else if (/meiju/.test(ktab)) {
			kurl = `http://82.156.24.206:12345/jx/jxm.php?url=${kurl}`;
			kurl = JSON.parse(fetch(kurl)).url;
			input = { jx: 0, parse: 0, url: kurl, header: { 'User-Agent': MOBILE_UA, 'Referer': 'https://m3u8.com'} }
		} else if (/madou/.test(ktab)) {
			kurl = `http://116.211.150.40:5555/cg/jx.php?id=${kurl}`;
			kurl = JSON.parse(fetch(kurl)).url;
			//input = { jx: 0, parse: 0, url: kurl, header: { 'User-Agent': MOBILE_UA, 'Referer': 'https://m3u8.com'} }
			if (/url=/.test(kurl)) {
				kurl = kurl.split('url=')[1];
				input = { jx: 0, parse: 1, url: kurl, header: rule.headers }
			} else{
				input = { jx: 0, parse: 0, url: kurl, header: { 'User-Agent': MOBILE_UA, 'Referer': 'https://m3u8.com'} }
			}
		} else {
			input = { jx: 0, parse: 1, url: kurl }
		}
	}),

	filter: 'H4sIAAAAAAAAA+2Z31MaSRDH3/krUvvMw6waxfwrqTyQK54uyUPMXZVlWQUhovgD0LuACXuKEkGIxp9lYBH8Z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